package com.br.nninn.bean;

import java.util.List;

public class Map {
	
	private static final String html1 = 
			
			"<!DOCTYPE html><html>" +
			"<head>" +
			"<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />" +
			"<style type=\"text/css\"> " +
				"html { height: 100% }" +
				"body { height: 100%; margin: 0; padding: 0 }" +
				"#map_canvas { height: 100% }" +
			"</style>" +
			"<title>NninnServer</title>  " +
			"<script src=\"http://maps.google.com/maps/api/js?sensor=false\" type=\"text/javascript\"></script>" +
			"</head> " +
			"<body> " +
			"<script type=\"text/javascript\">" +
				"function initialize() {" +
					"var locations = [";
	
	private static final String html2 = 
			
			"var map = new google.maps.Map(document.getElementById('map_canvas'), {" +
			"zoom: 15," +
			"center: new google.maps.LatLng(";
	
	private static final String html3 = 
			
			"mapTypeId: google.maps.MapTypeId.ROADMAP});" +
			
			"var infowindow = new google.maps.InfoWindow();" +
			
			"var marker, i;" +
			
			"for (i = 0; i < locations.length; i++) {" +
				"marker = new google.maps.Marker({" +
					"position: new google.maps.LatLng(locations[i][1], locations[i][2])," +
					"map: map," +
					"title: locations[i][0]" +
				"});" +
					
			
				"google.maps.event.addListener(marker, 'click', (function(marker, i) {" +
					"return function() {" +
						"infowindow.setContent(locations[i][4]);" +
						"infowindow.open(map, marker);}" +
					"}" +
				")(marker, i));" +
			"}}" +
	
		"</script>" +
		
		"<body onload=\"initialize()\">" +
			"<div id=\"map_canvas\" style=\"width:100%; height:100%\"></div>" +
		"</body></html>";
	
	
	private StringBuilder html = new StringBuilder();
	
	
	public Map(String latitude, String longitude, List<Hotel> hotelList) {
		
		this.html.append(html1);
		
		appendHotels(hotelList);
		
		this.html.append(html2).append(latitude).append(",").append(longitude).append("),").append(html3);
		
		
	}

	private void appendHotels(List<Hotel> hotelList) {
		
		int i = 0;
		
		System.out.println("Hotel list size = " + hotelList.size());
		
		for (Hotel hotel : hotelList) {
			
			if (i < hotelList.size()) {
				
				 String contentString = "<div id=\"content\">"+
						    "<div id=\"siteNotice\">"+
						    "</div>"+
						    "<h2 id=\"firstHeading\" class=\"firstHeading\">" + hotel.getName() + "</h2>"+
						    "<div id=\"bodyContent\">"+
						    "<p>" + hotel.getShortDescription() + "</p>"+
						    "<p>Address: " + hotel.getAddress() + 
						    	", " + hotel.getCity() +
						    	", " + hotel.getStateProvinceCode() +
						    	", " + hotel.getCountryCode() + ". " +
						    	"Postal code: " + hotel.getPostalCode() + "." +
						    "</p>" +
						    "<p>Low rate: " + hotel.getLowRate() + " and High rate: " + hotel.getHighRate() + ". " +	
						    "Hotel rating: " + hotel.getHotelRating() + ".<br/>" +
//						    "Hotel proximity distance: " + hotel.getProximityDistance() + ". " +
//						    "Latitude: " + hotel.getLatitude() + " and Longitude: " + hotel.getLongitude() +
						    "</p>" +
						    "</div>"+
						    "</div>";
				
				String temp = "['" 
						+ hotel.getName() + "'," 
						+ hotel.getLatitude() + "," 
						+ hotel.getLongitude() + "," 
						+ hotelList.indexOf(hotel) + ",'"
						+ contentString + "'";
				
				String fim = "],";
				
				if (i == hotelList.size() - 1) {
					fim = "]];";
				}
				
				temp = temp + fim;
				
				this.html.append(temp);
				i = i + 1;
			} else {
				break;
			}
		}
	}
	
	public String toString() {
		return this.html.toString();
	}
	
	

}
